package com.boarsoft.boar.config.lock.biz.impl;

import java.util.List;

import com.boarsoft.common.dao.PagedResult;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.config.lock.LockInfo;
import com.boarsoft.boar.config.lock.biz.LockInfoBiz;
import com.boarsoft.common.Util;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class LockInfoBizImpl extends SimpleBizImpl implements LockInfoBiz {
    @Override
    @Transactional(
            readOnly = true
    )
    public List<LockInfo> find(String appId, String url, String address, String type) {
        StringBuilder sb = new StringBuilder();
        if (Util.strIsNotEmpty(appId)) {
            sb.append(" and vAppId = '").append(appId).append("'");
        }
        if (Util.strIsNotEmpty(url)) {
            sb.append(" and vUrl = '").append(url).append("'");
        }
        if (Util.strIsNotEmpty(address)) {
            sb.append(" and vAddress = '").append(address).append("'");
        }
        if (Util.strIsNotEmpty(type)) {
            sb.append(" and vType = '").append(type).append("'");
        }
        sb.append(" order by cId");
        return this.dao.list(LockInfo.class,sb.toString());
    }

    @Transactional
    @Override
    public void delete(LockInfo lockInfo) {
        this.dao.delete(lockInfo);
    }
    @Transactional
    @Override
    public void save(LockInfo lockInfo) {
        if (Util.strIsEmpty(lockInfo.getId())) {
           this.dao.save(lockInfo);
        } else {
            this.dao.merge(lockInfo);
        }
    }
    @Override
    public PagedResult<LockInfo> list(String appId, String url, String address, String type, String orderBy, int pageNo, int pageSize) {
        StringBuilder sb = new StringBuilder();
        if (Util.strIsNotEmpty(appId)) {
            sb.append(" and vAppId = '").append(appId).append("'");
        }
        if (Util.strIsNotEmpty(url)) {
            sb.append(" and vUrl = '").append(url).append("'");
        }
        if (Util.strIsNotEmpty(address)) {
            sb.append(" and vAddress = '").append(address).append("'");
        }
        if (Util.strIsNotEmpty(type)) {
            sb.append(" and vType = '").append(type).append("'");
        }
        if (Util.strIsNotEmpty(orderBy)) {
            sb.append(" order by").append(orderBy);
        }
        return this.dao.list2(LockInfo.class, sb.toString(), pageNo, pageSize);
    }
}
